package com.liull.netty.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 12:04 2020/9/26
 * @Modified By:
 */
public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        // websocket本身基于http协议 因此需要使用http编解码器
        pipeline.addLast(new HttpServerCodec());
        // 基于块传输大文件的处理器 用来保证处理器状态
        pipeline.addLast(new ChunkedWriteHandler());
        // 将HttpMessage和随后的HttpContent聚合为一个FullHttpRequest或者FullHttpResponse
        // 放置在httpServerCode之后忽略HttpContent
        pipeline.addLast(new HttpObjectAggregator(8192));
        pipeline.addLast(new WebSocketServerProtocolHandler("/websocket"));
        pipeline.addLast(new WebSocketHandler());
    }
}
